package com.zhuxulin.java.email.mail;

import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import com.zhuxulin.java.email.mail.bean.SimpleEmailMessage;

public class SendSimpleEmail {
	private static SendSimpleEmail mail = new SendSimpleEmail();
	private static final String HOST = "smtp.qq.com";
	private static boolean isSSL = true;
	private static final String USER_NAME = "991777105";
	private static final String USER_PASSWORD = "aiai1989";
	/** 接受到邮件时显示的名字 */
	private static final String ACCOUNT_NAME = "ZhuXu";
	private Email email = null;

	private SendSimpleEmail() {
	}

	public static SendSimpleEmail getInstance() {
		return mail;
	}

	public void sendMail() throws EmailException {
		// SimpleEmail email = new SimpleEmail();
		email.setHostName(HOST);
		email.setFrom(USER_NAME + "@qq.com", ACCOUNT_NAME);
		email.addTo("501318740@qq.com");
		// email.addTo(new String[] { "501318740@qq.com", "501318740@qq.com",
		// "501318740@qq.com" });
		email.setSSLOnConnect(isSSL);
		email.setAuthentication(USER_NAME, USER_PASSWORD);
		email.setMsg("Hello You are my love");
		email.setSubject("Love is now");
		email.send();
	}

	public synchronized void sendMail(SimpleEmailMessage msg) throws EmailException, InterruptedException {
		email = new SimpleEmail();
		email.setHostName(HOST);
		email.setFrom(USER_NAME + "@qq.com", msg.getName());
		email.addTo(msg.getTo());
		email.setSSLOnConnect(isSSL);
		email.setAuthentication(USER_NAME, USER_PASSWORD);
		email.setMsg(msg.getMsg());
		email.setSubject(msg.getSubject());
		Thread.sleep(msg.getDelay() * 1000);
		email.send();
	}
}
